const bcrypt = require('bcryptjs')

const jwt = require('jsonwebtoken')



const hash = (password) => {
    return new Promise((resolve, reject) => {
        bcrypt.hash(password, 10, function(err, res) {
            if(err) {
                reject(err)
            }
            resolve(res)
        })
    })
}

const compare = (password, myPlaintextPassword) => {
    return new Promise((resolve, reject) => {
        bcrypt.compare(password, myPlaintextPassword, function(err, res) {
            if(err) {
                reject(err)
            }
            resolve(res)
        })
    })
}

const sign = (username) => {
    const tk = jwt.sign({username: username}, 'i love you')
    return tk
}

const verify = (tk) => {
    try {
        return jwt.verify(tk, 'i love you')
    } catch (err) {
        console.log(err)
    }
}

module.exports = {
    hash,
    compare,
    sign,
    verify,
}